package cn.jdy.anquan.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jdy.anquan.entity.SiteBook;
import cn.jdy.framework.util.StringUtil;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.anquan.entity.Report;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import org.springframework.util.Assert;

@Service
public class ReportService extends BaseDao<Report> {
  /**
   * 查询恶意网址记录数
   * @param bo
   * @return
   */
  public int getCount(Report bo){
    try {
      DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
      if(bo.getReason()>0)  {
        dc.add(Restrictions.eq("reason", bo.getReason()));
      }
      return super.count(dc).intValue();
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "查询恶意网址记录数！", e);
    }
    return 0;
  }
  
  /**
   * 查询恶意网址记录
   * @param bo
   * @return
   */
  public List<Report> query(Report bo) {
    List<Report> list = null;
    try {
      String sql ="select id,website,reason,sonReason,detailReason,telphone,qq,status,createTime  from report t";
        if(bo.getReason()>0){
          sql+=" where  t.reason = "+bo.getReason();
        }
        sql+=" order by t.createTime desc  limit "+bo.getiDisplayStart()+" , "+bo.getiDisplayLength();

      SQLQuery query = em().createSQLQuery(sql).addEntity(Report.class);
      list = query.list();
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "查询评论记录失败！", e);
    }
    return list;
  }

  public Map<String, Object> queryByPage(Report report, String fromDate, String toDate) {
    Assert.notNull(report);
    DetachedCriteria dc = DetachedCriteria.forClass(super.entityClass);
    if(StringUtil.isNotEmpty(report.getQq())){
      dc.add(Restrictions.eq("qq", report.getQq()));
    }
    if(StringUtil.isNotEmpty(report.getTelphone())) {
      dc.add(Restrictions.eq("telPhone", report.getTelphone()));
    }
    if(StringUtil.isNotEmpty(report.getWebsite())) {
      dc.add(Restrictions.eq("website", report.getWebsite()));
    }
    if(report.getReason() > 0) {
      dc.add(Restrictions.eq("reason", report.getReason()));
    }
    if(report.getSonReason() > 0) {
      dc.add(Restrictions.eq("sonReason", report.getSonReason()));
    }
    if(report.getStatus() > 0) {
      dc.add(Restrictions.eq("status", report.getStatus()));
    }
    if(StringUtil.isNotEmpty(fromDate)){
      if(StringUtil.isNotEmpty(toDate)){
        dc.add(Restrictions.between("createTime", fromDate, toDate));
      }else {
        dc.add(Restrictions.gt("createTime", fromDate));
      }
    }else if(StringUtil.isNotEmpty(toDate)){
      dc.add(Restrictions.lt("createTime", fromDate));
    }

    dc.addOrder(Order.desc("id"));

    Map<String, Object> resp = new HashMap<>();
    resp.put("dataList", super.find(dc, report.getiDisplayStart(), report.getiDisplayLength()));
    Long count = super.count(dc);
    resp.put("iTotalDisplayRecords", count);
    resp.put("iTotalRecords", count);
    return resp;
  }

}
